#include "OSAPI.h"

using namespace os;

Thread::Thread(Thread::ThreadPriority pri, std::string nme)
	: priority(pri), name(nme)
{}

Thread::~Thread()
{
	pthread_cancel(handle);
}

void Thread::start()
{
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setschedpolicy(&attr,SCHED_RR);
	sched_param sched;
	sched.sched_priority = getPriority();
	pthread_attr_setschedparam(&attr,&sched);
	int res = pthread_create(&handle, &attr, threadMapper,(void*)this);
	if (res != 0)
	{
		perror("Thread creation failed");
		exit(1);
	}
	//setPriority(getPriority());
}

/*void Thread::setPriority(Thread::ThreadPriority pri)
{
priority = pri;
//int ret = pthread_setsche	dprio(handle, pri);
if (ret != 0)
{
perror("Priority handling did not take place");
return false;
}
return true;
}*/

Thread::ThreadPriority Thread::getPriority()
{
	return priority;
}

std::string Thread::getName()
{
	return name;
}

void* Thread::threadMapper(void* att)
{
	((Thread*)att)->run();
}
